/*
 * Copyright 2020 edenrun.cn All right reserved. This software is the
 * confidential and proprietary information ofedenrun.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with edenrun.cn.
 */

package com.zoom.pluto.smf.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zoom.pluto.common.model.dto.SmfDTO;
import com.zoom.pluto.common.util.HttpClientUtils;
import com.zoom.pluto.common.util.Xml2JsonUtils;
import com.zoom.pluto.smf.service.SmfClientService;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SMF客户端服务实现类
 *
 * @author zoom
 * @date 2020-06-09
 */
@Slf4j
@Service
public class SmfClientServiceImpl implements SmfClientService {

    private final static String SMF_URL = "http://192.168.192.2:99/SMFWebservice.asmx/GetTable?binglino=%s";

    @Override
    public SmfDTO querySmfDetailByPatNo(String patno) {
        try {
            String url = String.format(SMF_URL, patno);
            log.info("请求smf地址-{}", url);
            String result = HttpClientUtils.doGet(url, null);
            //            Files.write(Paths.get("/Users/zoom/smf"), result.getBytes(Charset.forName("UTF-8")));

            Document doc = DocumentHelper.parseText(result);
            //            String xpath = "/DataTable/diffgr/NewDataSet/TABLE[@diffgr:id='TABLE1']";
            String xpath = "/DataTable/*/NewDataSet/TABLE";
            List<Element> composites = doc.selectNodes(xpath);
            if(null == composites || composites.size() == 0) {
                return new SmfDTO();
            }
            JSONObject json = new JSONObject();
            Xml2JsonUtils.dom4j2Json(composites.get(0), json);
            JSONArray smfList = (JSONArray) json.get("TABLE");
            SmfDTO smfDTO = JSON.parseObject(JSON.toJSONString(json), SmfDTO.class);
            log.info("获取smf信息列表-{}", JSON.toJSONString(smfDTO));
            //
            //            Document doc = DocumentHelper.parseText(result);
            //            JSONObject json = new JSONObject();
            //            Xml2JsonUtils.dom4j2Json((Element) ((Element) doc.getRootElement().content().get(2)).content().get(2), json);
            //
            //            JSONArray smfList = (JSONArray) json.get("TABLE");
            //            SmfDTO smfDTO = JSON.parseObject(JSON.toJSONString(smfList.get(0)), SmfDTO.class);
            //
            //            log.info("获取smf信息列表-{}", JSON.toJSONString(smfDTO));
            return smfDTO;
        } catch(Exception e) {
            log.error("获取smf信息异常", e);
            return new SmfDTO();
        }
    }

    //    @Override
    //    public List<SmfDetailResDTO> querySmfDetailListByBinglino(String binglino) {
    //        String methodName = "GetTable";
    //        String result = WebServiceUtils.callWebService(smfUrl, methodName, binglino);
    //        log.info("根据病历号「{}」取得病人多次的就诊信息:{}", binglino, result);
    //        return new ArrayList<>();
    //    }

    //    public static void main(String[] args) {
    //        try {
    //            List<String> contents = Files.readAllLines(Paths.get("/Users/zoom/smf"), Charset.forName("UTF-8"));
    //            String result = "";
    //            for(String content : contents) {
    //                result = result + content;
    //            }
    //            Document doc = DocumentHelper.parseText(result);
    //            //            String xpath = "/DataTable/diffgr/NewDataSet/TABLE[@diffgr:id='TABLE1']";
    //            String xpath = "/DataTable/*/NewDataSet/TABLE";
    //            List<Element> composites = doc.selectNodes(xpath);
    //            if(null == composites || composites.size() == 0) {
    //                return;
    //            }
    //            JSONObject json = new JSONObject();
    //            Xml2JsonUtils.dom4j2Json(composites.get(0), json);
    //            JSONArray smfList = (JSONArray) json.get("TABLE");
    //            SmfDTO smfDTO = JSON.parseObject(JSON.toJSONString(json), SmfDTO.class);
    //            log.info("获取smf信息列表-{}", JSON.toJSONString(smfDTO));
    //        } catch(Exception e) {
    //            log.error("exception", e);
    //        }
    //    }
}
